package com.bakar.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.bakar.util.BakarConstants;
import com.bakar.util.DBUtil;
import com.bakar.util.UserVOComparator;
import com.bakar.valueobjects.EmailVO;
import com.bakar.valueobjects.FriendVO;
import com.bakar.valueobjects.UserVO;

public class UserJPADAO implements UserDAO {

	@Override
	public void createUser(UserVO userVO) {
		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		// EntityTransaction tx = em.getTransaction();
		// tx.begin();
		em.persist(userVO);
		// tx.commit();
		em.close();

	}

	@Override
	public void updateUser(UserVO userVO) {
		UserVO userVOFrmDB = getUser(userVO);

		userVOFrmDB.setImagePath(userVO.getImagePath());
		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		// EntityTransaction tx = em.getTransaction();
		// tx.begin();
		em.merge(userVOFrmDB);
		// tx.commit();
		em.close();

	}

	@Override
	public UserVO getUser(UserVO userVO) {
		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		UserVO userVOFrmDB = em.find(UserVO.class, userVO.getLoginId());
		em.close();
		return userVOFrmDB;
	}

	@Override
	public void updateStatus(UserVO userVO) {
		UserVO userVOFrmDB = getUser(userVO);
		userVOFrmDB.setStatus(userVO.getStatus());

		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		/*
		 * EntityTransaction tx = em.getTransaction(); tx.begin();
		 */
		em.merge(userVOFrmDB);
		// tx.commit();
		em.close();

	}

	@Override
	public void addFriend(FriendVO friend) {
		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		/*
		 * EntityTransaction tx = em.getTransaction(); tx.begin();
		 */
		em.persist(friend);
		/* tx.commit(); */
		em.close();

	}

	@Override
	public void updateFriend(FriendVO friend) {
		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		/*
		 * EntityTransaction tx = em.getTransaction(); tx.begin();
		 */
		em.merge(friend);
		/* tx.commit(); */
		em.close();

	}

	@Override
	public FriendVO getFriend(FriendVO friend) {
		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		List<FriendVO> friends = em.createQuery(
				"select f from Friends f where ownerid=?1 and friendid=?2")
				.setParameter(1, friend.getOwnerid()).setParameter(2,
						friend.getFriendid()).getResultList();
		FriendVO result = friends.get(0);
		result.getId();
		em.close();
		return result;

	}

	@Override
	public List<UserVO> getOnlineFriends(UserVO user) {
		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		List<FriendVO> friends = em
				.createQuery(
						"select f from Friends f where ownerid=?1 and status<>?2 order by status asc, friendid asc  ")
				.setParameter(1, user.getLoginId()).setParameter(2, BakarConstants.FRND_REJ).getResultList();
		List<UserVO> users = new ArrayList<UserVO>();
		for (FriendVO friend : friends) {
			if (friend.getFriendid() != null) {
				// if the friend is not alread register then value
				// returned would be null
				UserVO friendUser = getUser(new UserVO(friend.getFriendid()));
				if (friendUser != null) {
					friendUser.setFrndStatus(friend.getStatus());

					users.add(friendUser);
				}

			}

		}
		em.close();
		return users;
	}

	/*
	 * public static void main(String... args) { UserJPADAO dao = new
	 * UserJPADAO(); UserVO user = new UserVO("Yogesh1111111");
	 * System.out.println(dao.getUser(user).getMyFriends()); }
	 */

	@Override
	public void updateDisplayName(UserVO userVO) {
		UserVO userVOFrmDB = getUser(userVO);
		userVOFrmDB.setDisplayName(userVO.getDisplayName());

		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		/*
		 * EntityTransaction tx = em.getTransaction(); tx.begin();
		 */
		em.merge(userVOFrmDB);
		// tx.commit();
		em.close();

	}

	public static void main(String... args) {
		// create an ArrayList object15.
		ArrayList arrayList = new ArrayList();

		// Add elements to Arraylist
		arrayList.add(new UserVO("Yogesh"));
		arrayList.add(new UserVO("Amit"));
		arrayList.add(new UserVO("Sachin"));
		arrayList.add(new UserVO("Mohit"));
		arrayList.add(new UserVO("Ram"));
		System.out.println(arrayList);
		Collections.sort(arrayList, new UserVOComparator());
		System.out.println(arrayList);
	}

	@Override
	public void createEmail(EmailVO emailVO) {
		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		// EntityTransaction tx = em.getTransaction();
		// tx.begin();
		em.persist(emailVO);
		// tx.commit();
		em.close();

	}

	@Override
	public List<UserVO> getGroupApprovedUsers(UserVO user) {
		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		List<FriendVO> friends = em.createQuery(
				"select f from Friends f where friendid=?1 and status=?2")
				.setParameter(1, user.getLoginId()).setParameter(2,
						BakarConstants.FRND_APPROVED).getResultList();
		List<UserVO> users = new ArrayList<UserVO>();
		for (FriendVO friend : friends) {
			if (friend.getFriendid() != null) {
				// if the friend is not alread register then value
				// returned would be null
				UserVO friendUser = getUser(new UserVO(friend.getOwnerid()));
				if (friendUser != null) {
					friendUser.setFrndStatus(friend.getStatus());

					users.add(friendUser);
				}

			}

		}
		em.close();
		return users;
	}

	@Override
	public List<UserVO> getGroupUsers(UserVO user) {
		EntityManager em = DBUtil.getInstanceEntityManagerFactory()
				.createEntityManager();
		List<FriendVO> friends = em.createQuery(
				"select f from Friends f where friendid=?1 and status=?2")
				.setParameter(1, user.getLoginId()).getResultList();
		List<UserVO> users = new ArrayList<UserVO>();
		for (FriendVO friend : friends) {
			if (friend.getFriendid() != null) {
				// if the friend is not alread register then value
				// returned would be null
				UserVO friendUser = getUser(new UserVO(friend.getOwnerid()));
				if (friendUser != null) {
					friendUser.setFrndStatus(friend.getStatus());

					users.add(friendUser);
				}

			}

		}
		em.close();
		return users;
	}

}
